import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';

import { fetchValidation } from '@/utils';
import {queryDetailInfo } from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    detail: Effect;
  };
  reducers: {
    saveDetail: Reducer;
    clear:Reducer;
  };
}

const Model: ModelType = {
  namespace: 'logisCenterBatchPrint',

  state: {
    detailInfo: {},
    spinning:true
  },

  effects: {
    *detail({ payload }, { call, put }) {
      const { status, ...query } = payload;
      const response = yield call(queryDetailInfo, query);
      const data = fetchValidation(response);
      const params = {
        ...data.body,
        status,
      };
      yield data &&
        put({
          type: 'saveDetail',
          payload: params,
        });
    },
  },

  reducers: {
    saveDetail(state, { payload }) {
      return {
        ...state,
        detailInfo: payload,
        spinning:false
      };
    },
    clear() {
      return {
        detailInfo: {},
        spinning:true,
      };
    },
  },
};

export default Model;
